﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using SqlLibrary;
using Ecopsy.DataTier;
using Ecopsy.Tools;

public partial class Ctl_ctlClientListSelector : System.Web.UI.UserControl
{
    private const string Key                = "ctlClientListSelector";
    protected  const int PageSize           = 4;
    private List<Client> RS                 = new List<Client>();

    public Func<Client,bool> OnSelectedChange;
    public Func<Client, bool> OnAddRecord;
    public Func< bool> OnCancel;

    public Client Client { get; set; }

    protected void Page_Load(object sender, EventArgs e){}
    
    protected override void OnDataBinding(EventArgs e){}

    public void MyDataBind()
    {
        txtFindStr.DataBind();
        ObjectDS_PF ODS = new ObjectDS_PF(GetList);
        lvClients.DataSource = ODS.ImAsObjectDataSource;

        lvClients.DataBind();
        DataPager.DataBind();
    }
    
    public Client[] GetList(int startRowIndex, int maximumRows, out int RowCount)                                         
    {

        ListWhere w = (ListWhere)Where;
        w.ListLoadStatment.StartRowIndex = startRowIndex;
        w.ListLoadStatment.EndRowIndex = maximumRows ;
        wsFace _ws_Face = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
        ClientList l = _ws_Face.GetClientList((ClientList.sqlLoadStatment)w.ListLoadStatment);
        RowCount = l.OutputRwCnt;
        RS = l.ToList<Client>();
        return RS.ToArray <Client>();

    }
    
    public ListWhere Where
    {
        get
        {
            ListWhere r = null;
            if (!CacheTools.IsSessionCacheObject(Key))
            {                                
                r = new ListWhere();
                r.ListLoadStatment = new ClientList.sqlLoadStatment();
                r.SortCondition.SortDirection = SortDirection.Ascending;
                r.SortCondition.SortField = "Name";
                CacheTools.SetSessionCacheObject(Key, r);
            }
            return (ListWhere)CacheTools.GetSessionCacheObect(Key);      
        }
    }


    protected void btnClose_OnClick(object sender, ImageClickEventArgs e)
    {
        if (iPageController.IsNewRecord(hfSelClient.Value))
        {
            wsFace ws = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
            Client c= ws.GetClient(hfSelClient.Value.Replace(GlobalConstants.qPrefNEW_RECORD, ""));
            
            if (!string.IsNullOrEmpty(c.FullName)) 
                Where.ListLoadStatment.Lexeme = c.FullName.Replace(" ", "%"); 
                
        }   
        MyDataBind();
    }
    protected void btnFind_Click(object sender, ImageClickEventArgs e)
    {
        Where.ListLoadStatment.Lexeme = txtFindStr.Text;
        MyDataBind();
    }
    
    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        if (null != OnCancel)
            OnCancel();
    }

    protected void btnEdit_OnClick(object sender, ImageClickEventArgs e)
    {
        string s = ((ImageButton)sender).CommandArgument;
        hfSelClient.Value = s; 
        ClientControlWrapper.Attributes.Add ("src", "iCRM_ClientEditor.aspx?ClientID="+s);
        pop_pnlClientEdit.Show();
    }

    protected void btnAdd_Click(object sender, ImageClickEventArgs e)
    {
        string s = Guid.NewGuid().ToString() + GlobalConstants.qPrefNEW_RECORD;
        hfSelClient.Value = s; 
        ClientControlWrapper.Attributes.Add("src", "iCRM_ClientEditor.aspx?ClientID="+s);
        pop_pnlClientEdit.Show();
    }


    protected void btnSel_Click(object sender, EventArgs e)
    {
        if (null == OnSelectedChange)
            return;
        wsFace ws = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
        Client = ws.GetClient(((LinkButton )sender).CommandArgument);
        OnSelectedChange(Client);
    }

    protected bool PagerVisible { get 
    {
        return RS.Count > PageSize-1; 
    } }
    protected void lvClients_PagePropertiesChanged(object sender, EventArgs e)
    {
        MyDataBind();
    }
    protected void btnSetEmptyRecord_Click(object sender, ImageClickEventArgs e)
    {
        OnSelectedChange(null);
    }
    
}
